<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Goods;
use App\Models\GoodsCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Goods AS GoodsModel;
use App\Models\Author AS AuthorModel;
use App\Models\GoodsCollection;
use App\Models\User;
class GoodsCollectionController extends AdminController
{
//    public function index(Content $content)
//    {
//        return $content->body(new GoodsTable());
//    }
    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        $user_id = \Request::get("user_id");
        $user = User::find($user_id);
        return $content
            ->title($user->nickname."的收藏列表")
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body($this->grid());
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Goods(['goodsCategory']), function (Grid $grid) {
            //某某的收藏的产品列表
            $goods_ids = GoodsCollection::where("user_id",\Request::get("user_id"))
                ->pluck("goods_id")
                ->toArray();
//            dd($goods_ids);
            $grid->model()->goods_ids = $goods_ids;
            //            collectionGoods
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column("goodsCategory.name","商品类别");
//            $grid->column("on_sale","是否在售")->switch(1,true);
            // 不写label会自动从翻译文件翻译，具体使用请参照 “字段翻译” 章节
// 禁用详情按钮
            $grid->disableViewButton();
            $grid->disableDeleteButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            // 也可以这样
            $grid->disableBatchDelete();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                // append一个操作 这里呢就是屏蔽原有的详情，加一个PC、手机端的详情
                $actions->disableView();
                $actions->row->id;//当前列的id
                $actions->append('<a href=""><i class="fa fa-eye"></i>显示</a>');

            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('name');
                $filter->equal('goods_category_id',"商品类别")
                    ->select(GoodsCategory::pluck("name","id")->toArray());
//                $filter->between('created_at', "添加日期")->datetime();

//                $filter->equal("on_sale","是否在售")
//                    ->select([
//                       1=>"是",
//                        0=>"否"
//                    ]);
//                $filter->equal("is_hot","是否特惠")
//                    ->select([
//                        1=>"是",
//                        0=>"否"
//                    ]);
//                $filter->equal("is_new","是否最新")
//                    ->select([
//                        1=>"是",
//                        0=>"否"
//                    ]);
//                $filter->equal("is_recommend","是否推荐")
//                    ->select([
//                        1=>"是",
//                        0=>"否"
//                    ]);
//                $filter->equal("is_seckill")
//                    ->select([
//                        1=>"是",
//                        0=>"否"
//                    ]);
            });
        });
    }

}
